package com.wontube.smr.dao.bo;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.mapper.MovieMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.bo.QueryItemParams;


/**
 * MovieToday Dao层
 * 
 * @author hewc@wondershare.cn
 * 
 */
public class MovieTodayDao {
	private MovieTodayDao() {
	}
	
	/**
	 * 今天最受欢迎音乐
	 * 
	 * @param pageInfo
	 * @return
	 */
	public static MovieItems selectMusics(String genres, PagenateInfo pageInfo) {
		return selectMusics(genres, null, pageInfo);
	}
	
	public static MovieItems selectMusics(QueryItemParams params, PagenateInfo pageInfo) {
		return selectMusics(params.getGenre(), params.getInitial(), pageInfo);
	}	
	public static MovieItems selectMusics(String genres, String initial, PagenateInfo pageInfo) {
		MovieItems items=new MovieItems(pageInfo);
		
		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
		
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("movie.movie_type=", Movie.TYPE_MUSIC)
			.addCriterion("movie.title_initial=", initial);
		ex.setOrderByClause("movie_today.sort_index asc");
		
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			
			MovieMapper movieMapper = session.getMapper(MovieMapper.class);
			if (StringUtils.isNullOrEmpty(genres)) {
				items.setItems(movieMapper.selectWithToday(ex, rowBounds));
				
				if(pageInfo.getPageNo() == 1){
					items.setTotalCount(movieMapper.countWithToday(ex));
				}				
				
			} else {
				items.setItems(movieMapper.selectWithTodayByGenre(genres, ex, rowBounds));				
				
				if(pageInfo.getPageNo() == 1){
					items.setTotalCount(movieMapper.countWithDayByGenre(genres, ex));
				}				
			}			
		} finally {
			session.close();
		}
		
		return items;
	}
}
